<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>switchyard.lib.address &#8212; Switchyard 2017.01.5 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '2017.01.5',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">Switchyard 2017.01.5 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for switchyard.lib.address</h1><div class="highlight"><pre>
<span></span><span class="n">__author__</span> <span class="o">=</span> <span class="s1">&#39;jsommers@colgate.edu&#39;</span>

<span class="kn">from</span> <span class="nn">ipaddress</span> <span class="k">import</span> <span class="n">IPv4Address</span><span class="p">,</span> <span class="n">IPv4Network</span><span class="p">,</span> <span class="n">IPv6Address</span><span class="p">,</span> <span class="n">IPv6Network</span><span class="p">,</span> <span class="n">ip_address</span>
<span class="kn">from</span> <span class="nn">enum</span> <span class="k">import</span> <span class="n">Enum</span>

<span class="c1"># make aliases for built-in ip_address class</span>
<span class="n">IPAddr</span> <span class="o">=</span> <span class="n">IPv4Address</span> 

<span class="kn">import</span> <span class="nn">struct</span>
<span class="kn">import</span> <span class="nn">socket</span>

<span class="c1"># EthAddr class modified from POX code, license below.</span>

<span class="c1"># Copyright 2011,2012,2013 James McCauley</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at:</span>
<span class="c1">#</span>
<span class="c1">#     http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>

<div class="viewcode-block" id="EthAddr"><a class="viewcode-back" href="../../../reference.html#switchyard.lib.address.EthAddr">[docs]</a><span class="k">class</span> <span class="nc">EthAddr</span> <span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An Ethernet (MAC) address type.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;__value&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__init__</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">addr</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
      <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">      Understands Ethernet address is various forms.  Hex strings, raw byte</span>
<span class="sd">      strings, etc.</span>
<span class="sd">      &quot;&quot;&quot;</span>
      <span class="c1"># Always stores as a bytes object of length 6</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">__value</span> <span class="o">=</span> <span class="kc">None</span>

      <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">):</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">__value</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">addr</span><span class="p">[:</span><span class="mi">6</span><span class="p">])</span>
      <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">EthAddr</span><span class="p">):</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">__value</span> <span class="o">=</span> <span class="n">addr</span><span class="o">.</span><span class="n">raw</span>
      <span class="k">elif</span> <span class="n">addr</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">__value</span> <span class="o">=</span> <span class="n">b</span><span class="s1">&#39;</span><span class="se">\x00</span><span class="s1">&#39;</span> <span class="o">*</span> <span class="mi">6</span>
      <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
          <span class="n">possible_separators</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">,</span><span class="s1">&#39;-&#39;</span><span class="p">)</span>
          <span class="k">for</span> <span class="n">sep</span> <span class="ow">in</span> <span class="n">possible_separators</span><span class="p">:</span>
              <span class="k">if</span> <span class="n">addr</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span> <span class="o">==</span> <span class="mi">5</span><span class="p">:</span>
                  <span class="bp">self</span><span class="o">.</span><span class="n">__value</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">([</span> <span class="nb">int</span><span class="p">(</span><span class="n">val</span><span class="p">,</span><span class="n">base</span><span class="o">=</span><span class="mi">16</span><span class="p">)</span> <span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">addr</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">sep</span><span class="p">)])</span>
                  <span class="k">break</span>

      <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">:</span>
          <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Expected ethernet address string to be 6 raw &quot;</span> 
                               <span class="s2">&quot;bytes or some hex&quot;</span><span class="p">)</span>
  
<div class="viewcode-block" id="EthAddr.isBridgeFiltered"><a class="viewcode-back" href="../../../reference.html#switchyard.lib.address.EthAddr.isBridgeFiltered">[docs]</a>    <span class="k">def</span> <span class="nf">isBridgeFiltered</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Checks if address is an IEEE 802.1D MAC Bridge Filtered MAC Group Address</span>

<span class="sd">        This range is 01-80-C2-00-00-00 to 01-80-C2-00-00-0F. MAC frames that</span>
<span class="sd">        have a destination MAC address within this range are not relayed by</span>
<span class="sd">        bridges conforming to IEEE 802.1D</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span>  <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="mh">0x01</span><span class="p">)</span>
    	    <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mh">0x80</span><span class="p">)</span>
    	    <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="mh">0xC2</span><span class="p">)</span>
    	    <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">==</span> <span class="mh">0x00</span><span class="p">)</span>
    	    <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">==</span> <span class="mh">0x00</span><span class="p">)</span>
    	    <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mh">0x0F</span><span class="p">))</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_bridge_filtered</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">isBridgeFiltered</span><span class="p">()</span>

<div class="viewcode-block" id="EthAddr.isGlobal"><a class="viewcode-back" href="../../../reference.html#switchyard.lib.address.EthAddr.isGlobal">[docs]</a>    <span class="k">def</span> <span class="nf">isGlobal</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if this is a globally unique (OUI enforced) address.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">isLocal</span><span class="p">()</span></div>

<div class="viewcode-block" id="EthAddr.isLocal"><a class="viewcode-back" href="../../../reference.html#switchyard.lib.address.EthAddr.isLocal">[docs]</a>    <span class="k">def</span> <span class="nf">isLocal</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if this is a locally-administered (non-global) address.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span> <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&amp;</span> <span class="mi">2</span><span class="p">)</span> <span class="k">else</span> <span class="kc">False</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_local</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">isLocal</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_global</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">isGlobal</span><span class="p">()</span>

<div class="viewcode-block" id="EthAddr.isMulticast"><a class="viewcode-back" href="../../../reference.html#switchyard.lib.address.EthAddr.isMulticast">[docs]</a>    <span class="k">def</span> <span class="nf">isMulticast</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if this is a multicast address.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">True</span> <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&amp;</span> <span class="mi">1</span><span class="p">)</span> <span class="k">else</span> <span class="kc">False</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_multicast</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">isMulticast</span><span class="p">()</span>

<div class="viewcode-block" id="EthAddr.toRaw"><a class="viewcode-back" href="../../../reference.html#switchyard.lib.address.EthAddr.toRaw">[docs]</a>    <span class="k">def</span> <span class="nf">toRaw</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">raw</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">raw</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the address as a 6-long bytes object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__value</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">packed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">raw</span>

<div class="viewcode-block" id="EthAddr.toTuple"><a class="viewcode-back" href="../../../reference.html#switchyard.lib.address.EthAddr.toTuple">[docs]</a>    <span class="k">def</span> <span class="nf">toTuple</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a 6-entry long tuple where each entry is the numeric value</span>
<span class="sd">        of the corresponding byte of the address.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">)</span></div>

<div class="viewcode-block" id="EthAddr.toStr"><a class="viewcode-back" href="../../../reference.html#switchyard.lib.address.EthAddr.toStr">[docs]</a>    <span class="k">def</span> <span class="nf">toStr</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">separator</span> <span class="o">=</span> <span class="s1">&#39;:&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the address as string consisting of 12 hex chars separated</span>
<span class="sd">        by separator.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">separator</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="s1">&#39;</span><span class="si">{:02x}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">))</span></div>

    <span class="k">def</span> <span class="nf">__str__</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">toStr</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="n">other</span> <span class="o">=</span> <span class="n">EthAddr</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">raw</span>

    <span class="k">def</span> <span class="nf">__lt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="n">other</span> <span class="o">=</span> <span class="n">EthAddr</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="o">&lt;</span> <span class="n">other</span><span class="o">.</span><span class="n">raw</span>

    <span class="k">def</span> <span class="nf">__hash__</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">hash</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span> <span class="o">+</span> <span class="s2">&quot;(&#39;&quot;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">toStr</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&quot;&#39;)&quot;</span>

    <span class="k">def</span> <span class="nf">__len__</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">6</span></div>


<span class="n">ethaddr</span> <span class="o">=</span> <span class="n">EthAddr</span>
<span class="n">macaddr</span> <span class="o">=</span> <span class="n">EthAddr</span>


<div class="viewcode-block" id="SpecialIPv6Addr"><a class="viewcode-back" href="../../../reference.html#switchyard.lib.address.SpecialIPv6Addr">[docs]</a><span class="k">class</span> <span class="nc">SpecialIPv6Addr</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
    <span class="n">UNDEFINED</span> <span class="o">=</span> <span class="n">ip_address</span><span class="p">(</span><span class="s1">&#39;::&#39;</span><span class="p">)</span>
    <span class="n">ALL_NODES_LINK_LOCAL</span> <span class="o">=</span> <span class="n">ip_address</span><span class="p">(</span><span class="s1">&#39;ff02::1&#39;</span><span class="p">)</span>
    <span class="n">ALL_ROUTERS_LINK_LOCAL</span> <span class="o">=</span> <span class="n">ip_address</span><span class="p">(</span><span class="s1">&#39;ff02::2&#39;</span><span class="p">)</span>
    <span class="n">ALL_NODES_INTERFACE_LOCAL</span> <span class="o">=</span> <span class="n">ip_address</span><span class="p">(</span><span class="s1">&#39;ff01::1&#39;</span><span class="p">)</span>
    <span class="n">ALL_ROUTERS_INTERFACE_LOCAL</span> <span class="o">=</span> <span class="n">ip_address</span><span class="p">(</span><span class="s1">&#39;ff01::2&#39;</span><span class="p">)</span></div>

<span class="c1">#ff02::1:3 link local multicast name resolution</span>
<span class="c1">#ff02::1:ff00:0/104 solicited-node</span>
<span class="c1">#ff02::2:ff00:0/104 node information query</span>


<div class="viewcode-block" id="SpecialIPv4Addr"><a class="viewcode-back" href="../../../reference.html#switchyard.lib.address.SpecialIPv4Addr">[docs]</a><span class="k">class</span> <span class="nc">SpecialIPv4Addr</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
    <span class="n">IP_ANY</span> <span class="o">=</span> <span class="n">ip_address</span><span class="p">(</span><span class="s2">&quot;0.0.0.0&quot;</span><span class="p">)</span>
    <span class="n">IP_BROADCAST</span> <span class="o">=</span> <span class="n">ip_address</span><span class="p">(</span><span class="s2">&quot;255.255.255.255&quot;</span><span class="p">)</span></div>


<span class="k">class</span> <span class="nc">SpecialEthAddr</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
    <span class="n">ETHER_ANY</span>            <span class="o">=</span> <span class="n">EthAddr</span><span class="p">(</span><span class="n">b</span><span class="s1">&#39;</span><span class="se">\x00\x00\x00\x00\x00\x00</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="n">ETHER_BROADCAST</span>      <span class="o">=</span> <span class="n">EthAddr</span><span class="p">(</span><span class="n">b</span><span class="s1">&#39;</span><span class="se">\xff\xff\xff\xff\xff\xff</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="n">BRIDGE_GROUP_ADDRESS</span> <span class="o">=</span> <span class="n">EthAddr</span><span class="p">(</span><span class="n">b</span><span class="s1">&#39;</span><span class="se">\x01\x80\xC2\x00\x00\x00</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="n">LLDP_MULTICAST</span>       <span class="o">=</span> <span class="n">EthAddr</span><span class="p">(</span><span class="n">b</span><span class="s1">&#39;</span><span class="se">\x01\x80\xc2\x00\x00\x0e</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="n">PAE_MULTICAST</span>        <span class="o">=</span> <span class="n">EthAddr</span><span class="p">(</span><span class="n">b</span><span class="s1">&#39;</span><span class="se">\x01\x80\xc2\x00\x00\x03</span><span class="s1">&#39;</span><span class="p">)</span> 
                                                   <span class="c1"># 802.1x Port Access Entity</span>
    <span class="n">NDP_MULTICAST</span>        <span class="o">=</span> <span class="n">EthAddr</span><span class="p">(</span><span class="n">b</span><span class="s1">&#39;</span><span class="se">\x01\x23\x20\x00\x00\x01</span><span class="s1">&#39;</span><span class="p">)</span> 
                                                   <span class="c1"># Nicira discovery multicast</span>


<span class="k">def</span> <span class="nf">netmask_to_cidr</span> <span class="p">(</span><span class="n">dq</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">  Takes a netmask as either an IPAddr or a string, and returns the number</span>
<span class="sd">  of network bits.  e.g., 255.255.255.0 -&gt; 24</span>
<span class="sd">  Raise exception if subnet mask is not CIDR-compatible.</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dq</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
    <span class="n">dq</span> <span class="o">=</span> <span class="n">IPv4Address</span><span class="p">(</span><span class="n">dq</span><span class="p">)</span>
  <span class="n">v</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">dq</span><span class="p">)</span>
  <span class="n">c</span> <span class="o">=</span> <span class="mi">0</span>
  <span class="k">while</span> <span class="n">v</span> <span class="o">&amp;</span> <span class="mh">0x80000000</span><span class="p">:</span>
    <span class="n">c</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="n">v</span> <span class="o">&lt;&lt;=</span> <span class="mi">1</span>
  <span class="n">v</span> <span class="o">=</span> <span class="n">v</span> <span class="o">&amp;</span> <span class="mh">0xffFFffFF</span>
  <span class="k">if</span> <span class="n">v</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Netmask </span><span class="si">%s</span><span class="s2"> is not CIDR-compatible&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">dq</span><span class="p">,))</span>
  <span class="k">return</span> <span class="n">c</span>

<span class="k">def</span> <span class="nf">cidr_to_netmask</span> <span class="p">(</span><span class="n">bits</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">  Takes a number of network bits, and returns the corresponding netmask</span>
<span class="sd">  as an IPAddr.  e.g., 24 -&gt; 255.255.255.0</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">v</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">&lt;&lt;</span> <span class="n">bits</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
  <span class="n">v</span> <span class="o">=</span> <span class="n">v</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="mi">32</span><span class="o">-</span><span class="n">bits</span><span class="p">)</span>
  <span class="k">return</span> <span class="n">IPAddr</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">parse_cidr</span> <span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">infer</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">allow_host</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">  Takes a CIDR address or plain dotted-quad, and returns a tuple of address</span>
<span class="sd">  and count-of-network-bits.</span>
<span class="sd">  Can infer the network bits based on network classes if infer=True.</span>
<span class="sd">  Can also take a string in the form &#39;address/netmask&#39;, as long as the</span>
<span class="sd">  netmask is representable in CIDR.</span>

<span class="sd">  FIXME: This function is badly named.</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="k">def</span> <span class="nf">check</span> <span class="p">(</span><span class="n">r0</span><span class="p">,</span> <span class="n">r1</span><span class="p">):</span>
    <span class="n">a</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">r0</span><span class="p">)</span>
    <span class="n">b</span> <span class="o">=</span> <span class="n">r1</span>
    <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">allow_host</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">a</span> <span class="o">&amp;</span> <span class="p">((</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">b</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)):</span>
      <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Host part of CIDR address is not zero (</span><span class="si">%s</span><span class="s2">)&quot;</span>
                         <span class="o">%</span> <span class="p">(</span><span class="n">addr</span><span class="p">,))</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">r0</span><span class="p">,</span><span class="mi">32</span><span class="o">-</span><span class="n">r1</span><span class="p">)</span>

  <span class="n">addr</span> <span class="o">=</span> <span class="n">addr</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
  <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">addr</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
    <span class="k">if</span> <span class="n">infer</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
      <span class="k">return</span> <span class="n">check</span><span class="p">(</span><span class="n">IPAddr</span><span class="p">(</span><span class="n">addr</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">addr</span> <span class="o">=</span> <span class="n">IPAddr</span><span class="p">(</span><span class="n">addr</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
    <span class="n">b</span> <span class="o">=</span> <span class="mi">32</span><span class="o">-</span><span class="n">infer_netmask</span><span class="p">(</span><span class="n">addr</span><span class="p">)</span>
    <span class="n">m</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="n">b</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span>
    <span class="k">if</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">addr</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">m</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
      <span class="c1"># All bits in wildcarded part are 0, so we&#39;ll use the wildcard</span>
      <span class="k">return</span> <span class="n">check</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="c1"># Some bits in the wildcarded part are set, so we&#39;ll assume it&#39;s a host</span>
      <span class="k">return</span> <span class="n">check</span><span class="p">(</span><span class="n">addr</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
  <span class="k">try</span><span class="p">:</span>
    <span class="n">wild</span> <span class="o">=</span> <span class="mi">32</span><span class="o">-</span><span class="nb">int</span><span class="p">(</span><span class="n">addr</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
  <span class="k">except</span><span class="p">:</span>
    <span class="c1"># Maybe they passed a netmask</span>
    <span class="n">m</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">IPAddr</span><span class="p">(</span><span class="n">addr</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
    <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">m</span> <span class="o">&amp;</span> <span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">31</span><span class="p">):</span>
      <span class="n">b</span> <span class="o">+=</span> <span class="mi">1</span>
      <span class="n">m</span> <span class="o">&lt;&lt;=</span> <span class="mi">1</span>
    <span class="k">if</span> <span class="n">m</span> <span class="o">&amp;</span> <span class="mh">0x7fffffff</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
      <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Netmask &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">addr</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="s2">&quot; is not CIDR-compatible&quot;</span><span class="p">)</span>
    <span class="n">wild</span> <span class="o">=</span> <span class="mi">32</span><span class="o">-</span><span class="n">b</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">wild</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">wild</span> <span class="o">&lt;=</span> <span class="mi">32</span><span class="p">):</span>
      <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Invalid mask length&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">check</span><span class="p">(</span><span class="n">IPAddr</span><span class="p">(</span><span class="n">addr</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">wild</span><span class="p">)</span>
  <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">wild</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">wild</span> <span class="o">&lt;=</span> <span class="mi">32</span><span class="p">):</span>
    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;Invalid mask length&quot;</span><span class="p">)</span>
  <span class="k">return</span> <span class="n">check</span><span class="p">(</span><span class="n">IPAddr</span><span class="p">(</span><span class="n">addr</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">wild</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">infer_netmask</span> <span class="p">(</span><span class="n">addr</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">  Uses network classes to guess the number of network bits</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">addr</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">addr</span><span class="p">)</span>
  <span class="k">if</span> <span class="n">addr</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
    <span class="c1"># Special case -- default network</span>
    <span class="k">return</span> <span class="mi">32</span><span class="o">-</span><span class="mi">32</span> <span class="c1"># all bits wildcarded</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">addr</span> <span class="o">&amp;</span> <span class="p">(</span><span class="mi">1</span> <span class="o">&lt;&lt;</span> <span class="mi">31</span><span class="p">))</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
    <span class="c1"># Class A</span>
    <span class="k">return</span> <span class="mi">32</span><span class="o">-</span><span class="mi">24</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">addr</span> <span class="o">&amp;</span> <span class="p">(</span><span class="mi">3</span> <span class="o">&lt;&lt;</span> <span class="mi">30</span><span class="p">))</span> <span class="o">==</span> <span class="mi">2</span> <span class="o">&lt;&lt;</span> <span class="mi">30</span><span class="p">:</span>
    <span class="c1"># Class B</span>
    <span class="k">return</span> <span class="mi">32</span><span class="o">-</span><span class="mi">16</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">addr</span> <span class="o">&amp;</span> <span class="p">(</span><span class="mi">7</span> <span class="o">&lt;&lt;</span> <span class="mi">29</span><span class="p">))</span> <span class="o">==</span> <span class="mi">6</span> <span class="o">&lt;&lt;</span> <span class="mi">29</span><span class="p">:</span>
    <span class="c1"># Class C</span>
    <span class="k">return</span> <span class="mi">32</span><span class="o">-</span><span class="mi">8</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">addr</span> <span class="o">&amp;</span> <span class="p">(</span><span class="mi">15</span> <span class="o">&lt;&lt;</span> <span class="mi">28</span><span class="p">))</span> <span class="o">==</span> <span class="mi">14</span> <span class="o">&lt;&lt;</span> <span class="mi">28</span><span class="p">:</span>
    <span class="c1"># Class D (Multicast)</span>
    <span class="k">return</span> <span class="mi">32</span><span class="o">-</span><span class="mi">0</span> <span class="c1"># exact match</span>
  <span class="c1"># Must be a Class E (Experimental)</span>
  <span class="k">return</span> <span class="mi">32</span><span class="o">-</span><span class="mi">0</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">Switchyard 2017.01.5 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2013-2017, Joel Sommers.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>